package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.condition.EquipmentAbnormalCondition;
import com.infoauto.opc.gateway.model.gtm4.CheckTheNumberOfUnits2;
import com.infoauto.opc.gateway.model.gtm4.CheckTheProcess;
import com.infoauto.opc.gateway.model.gtm4.ControlSignal;
import com.infoauto.opc.gateway.model.gtm4.EquipmentAbnormal;
import com.infoauto.opc.gateway.service.CheckTheNumberOfUnits2Service;
import com.infoauto.opc.gateway.service.CheckTheProcessService;
import com.infoauto.opc.gateway.service.EquipmentAbnormalService;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 设备异常
 */

@CrossOrigin
@RestController
@RequestMapping("/equipmentAbnormal")
public class EquipmentAbnormalController {
    @Autowired
    private EquipmentAbnormalService service;

    /**
     * 根据异常状态查询异常履历1，只查询昨天和今天的数据
     */
    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectByStates(Integer states){
        try{
            List<EquipmentAbnormal> list = service.selectByStates(states);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
   }

    /**
     * 看板页面显示的五条记录，定时查询最新的5条当天的设备异常的记录
     * @return
     */
    @RequestMapping(value="/selectLimit5" ,method= RequestMethod.GET)
    public Result selectEquipmentAbnormal(){
        try{
            List<EquipmentAbnormal> list = service.selectEquipmentAbnormal();
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 导出设备异常履历1
     * @param response
     * @param states
     * @throws Exception
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response,Integer states) throws Exception {
        //1.构造数据
        List<EquipmentAbnormal> list = service.selectByStates(states);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.构造sheet
        String[] titles = {"编号", "设备公司", "设备名称","备注", "发生时间","解除时间","PLC地址"};
        Sheet sheet = workbook.createSheet("设备异常履历1");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        sheet.setColumnWidth(1, 20 * 256);//设置第二列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 30 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : titles) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(1);
        Cell cell = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        int i = 1;
        export3(list, sheet, cellStyle, datasAi, formatter, i);
        String fileName = URLEncoder.encode(sheet.getSheetName()+formatter.format(new Date())+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }

    /**
     * 设备异常履历2列表查询，最多只显示10000条数据
     */
    @RequestMapping(value="/selectEaList" ,method= RequestMethod.GET)
    public Result selectEquipmentAbnormalList2(EquipmentAbnormalCondition equipmentAbnormalCondition){
        try{
            List<EquipmentAbnormal> list = service.selectEquipmentAbnormalList2(equipmentAbnormalCondition);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 导出设备异常履历2
     * @param response
     * @param equipmentAbnormalCondition
     * @throws Exception
     */
    @RequestMapping(value = "/export2", method = RequestMethod.GET)
    public void export2(HttpServletResponse response,EquipmentAbnormalCondition equipmentAbnormalCondition) throws Exception {
        //1.构造数据
        List<EquipmentAbnormal> list = service.selectEquipmentAbnormalList2(equipmentAbnormalCondition);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.构造sheet
        String[] titles = {"编号", "设备公司", "设备名称","备注", "发生时间","解除时间","PLC地址"};
        Sheet sheet = workbook.createSheet("设备异常履历2");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        sheet.setColumnWidth(1, 20 * 256);//设置第二列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 30 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : titles) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(1);
        Cell cell = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        int i = 1;
        export3(list, sheet, cellStyle, datasAi, formatter, i);
        String fileName = URLEncoder.encode(sheet.getSheetName()+formatter.format(new Date())+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }

    /**
     * 导出行列的具体信息实现
     * @param list
     * @param sheet
     * @param cellStyle
     * @param datasAi
     * @param formatter
     * @param i
     */
    private void export3(List<EquipmentAbnormal> list, Sheet sheet, CellStyle cellStyle, AtomicInteger datasAi, SimpleDateFormat formatter, int i) {
        Cell cell;
        if(list!=null && list.size()>0){
            for (EquipmentAbnormal report : list) {
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //设备公司
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getEquipmentCompany());
                cell.setCellStyle(cellStyle);
                //设备名称
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getEquipmentName());
                cell.setCellStyle(cellStyle);
                //备注
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getRemark());
                cell.setCellStyle(cellStyle);
                //发生时间
                cell = dataRow.createCell(4);
                cell.setCellValue(formatter.format(report.getStartTime()));
                cell.setCellStyle(cellStyle);
                //解除时间
                cell = dataRow.createCell(5);
                if(report.getRemoveTime()!=null){
                    cell.setCellValue(formatter.format(report.getRemoveTime()));
                    cell.setCellStyle(cellStyle);
                }else{
                    cell.setCellValue("");
                    cell.setCellStyle(cellStyle);
                }
                //PLC地址
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getPlcAddress());
                cell.setCellStyle(cellStyle);

            }
        }
    }

}
